from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField, SelectField
from wtforms.validators import Length, DataRequired, Regexp
from app.models import TypeTel


# 类型表单
class TypeForm(FlaskForm):
    name = StringField('类别', validators=[DataRequired()])
    submit = SubmitField('提交')


# 添加联系人表单
class AddTelForm(FlaskForm):
    name = StringField("姓名", validators=[DataRequired()])
    tel = StringField("电话", validators=[DataRequired(), Regexp(r'^1(3|4|5|6|7|8|9)\d{9}$', message="电话不合法")])
    typetel_id = SelectField("类型", coerce=int)
    submit = SubmitField('提交')

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.typetel_id.choices = [(v.id, v.name) for v in TypeTel.query.all()]

